<?php if (!defined('FIT_PATH')) exit('No direct script access allowed');
/**
 * @copyright 2009 - 2011 Bincent.com. All Rights Reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @package FitPHP
 * @subpackage database
 * 
 * @author Hongbin Hsu <hongbin.hsu@gmail.com> Initial.
 * @version $Id: database/abstract.php 1.0 2009-07-18 13:34 hongbin.hsu $
 */
 
// ------------------------------------------------------------------------

abstract class Database_Abstract
{
	public $hostname = null;		//数据库服务器
    public $username = null;		//数据库帐号
    public $password = null;		//数据库密码
    public $database = null;		//数据库名称
    public $hostport = null;		//数据库端口
    public $charset = null;			//字符集
    public $dsn = null;				//DSN配置
    public $pconnect = false;		//是否使用持久连接
    protected $conn = null;			//数据库连接句柄
    protected $result = null;		//数据库结果集
    
    //----------------------------------------------------------
    public function __construct($config = array())
    {
    	if (!empty($config) && is_array($config)) {
    		foreach($config as $key => $value){
				$this->$key = $value;
			}
    	}
    	$this->connect();
    }
    
    //----------------------------------------------------------
    protected function connect(){}
    
    //----------------------------------------------------------
    protected function free(){}
    
    //----------------------------------------------------------
    protected function close(){}
    
    //----------------------------------------------------------
    /**
     * 插入数据库记录操作
     *
     * @param string $table
     * @param array $data
     * @return boolean
     */
    public function insert($table, $data = array())
    {
    	try {
    		if (!is_array($data)) {
    			throw new Exception("Insert {$table} data is not array.");
    		}
    		$insert = $values = null;
	        foreach ($data as $key => $value) {
	        	$insert .= "`". $key . "`,";
	        	$values .= "'". $value ."',";
	        }
	        $insert = rtrim($insert,',');
	        $values = rtrim($values,',');
	        $sql = "INSERT INTO {$table} ({$insert}) VALUES ({$values})";
	        return ($insert && $values) ? $this->query($sql): false;
    	}catch (Exception $e){
    		echo $e->getMessage();
    	}
    }
    
    //----------------------------------------------------------
    /**
     * 更新数据库记录操作
     *
     * @param string $table
     * @param array $data
     * @param string $where
     * @param int $limit
     * @return boolean
     */
    public function update($table, $data = array(), $where, $limit = 1)
    {
    	try {
    		if (!is_array($data)) {
    			throw new Exception("Update {$table} data is not array!");
    		}
	    	$fields = null;
	        foreach ($data as $key => $value) {
	        	$fields .= " `" . $key . "` = '" . $value . "',";
	        }
	        $fields = rtrim($fields,',');
	        $limit  = intval($limit);
			$sql = "UPDATE {$table} SET {$fields} WHERE {$where} LIMIT {$limit}";
	        return $fields ? $this->query($sql) : false;
    	}catch (Exception $e){
    		echo $e->getMessage();
    	}
    }
    
    //----------------------------------------------------------
    public function __call($method, $arguments)
    {
    	echo "unfind function: {$method} and arguments";
    }
    
    //----------------------------------------------------------
    public function __destruct()
    {
    	$this->free();
    	$this->close();
    }
}